package com.tang.design_pattern.responsePattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tmh
 * @date 2024/3/25 20:09
 * @description
 */
@Component
public class OrderHandleChainService {

    @Autowired
    private List<OrderHandleIntercept> orderHandleInterceptList;

    public Order execute(Order context) {

        //根据sort方法的返回值来进行升序处理
        orderHandleInterceptList = orderHandleInterceptList.stream().sorted(Comparator.comparingInt(OrderHandleIntercept::sort)).collect(Collectors.toList());
        for (OrderHandleIntercept handleIntercept : orderHandleInterceptList) {
            context = handleIntercept.handle(context);
        }
        return context;
    }


}
